package com.iflytek.crvoice2txt.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.iflytek.crvoice2txt.R;
import com.iflytek.crvoice2txt.entity.RecorderEntity;
import com.iflytek.crvoice2txt.entity.RecorderResearchEntity;
import com.iflytek.crvoice2txt.util.FileUtil;
import com.iflytek.crvoice2txt.widget.SlideItem;

import java.util.ArrayList;
import java.util.List;

public class SearchItemAdapter extends BaseAdapter {

    public interface OnSearchItemOperation {
        void onSearchItemDeleted(int position);
    }

    private OnSearchItemOperation onSearchItemOperation;

    public void setOnSearchItemOperation(OnSearchItemOperation onSearchItemOperation) {
        this.onSearchItemOperation = onSearchItemOperation;
    }

    private List<RecorderResearchEntity> list;
    private Context context;
    private LayoutInflater inflater;

    public SearchItemAdapter(Context context, List<RecorderResearchEntity> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            View content = inflater.inflate(R.layout.item_recorder_search, null);
            View menu = inflater.inflate(R.layout.adapter_item_menu, null);
            holder = new ViewHolder(content, menu);
            SlideItem slideItem = new SlideItem(context);
            slideItem.setContentView(content, menu);
            convertView = slideItem;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.menu_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(SearchItemAdapter.this.onSearchItemOperation!=null){
                    onSearchItemOperation.onSearchItemDeleted(position);
                }
            }
        });
        holder.tv_item_recorder_search_name.setText(list.get(position).recorderEntity.name);
        holder.tv_item_recorder_search_time.setText(list.get(position).recorderEntity.dateStr);
        holder.tv_item_recorder_search_size.setText(FileUtil.formatFileSizeToString(list.get(position).recorderEntity.size));
        return convertView;
    }

    class ViewHolder {

        TextView menu_delete;
        TextView tv_item_recorder_search_name;
        TextView tv_item_recorder_search_size;
        TextView tv_item_recorder_search_time;

        public ViewHolder(View center, View menu) {
            this.menu_delete = (TextView) menu.findViewById(R.id.menu_delete);
            tv_item_recorder_search_name = center.findViewById(R.id.tv_item_recorder_search_name);
            tv_item_recorder_search_size = center.findViewById(R.id.tv_item_recorder_search_size);
            tv_item_recorder_search_time = center.findViewById(R.id.tv_item_recorder_search_time);
        }
    }
}
